package define

import (
	cclouddefine "ce-cloud/pkg/define"
	"ce-cloud/pkg/log"
	"ce-cloud/pkg/service/p_go_micro"
	"ce-cloud/pkg/tools"
	"os"
)

// NewService go_micro 模板
//
//	param: argsSvc 新的p_go_micro.MicroService, argsSvc.Name必填
//	param: argsPath（可选参数） 如果填了则只取第一个值，否则走默认配置
//	returns: 组合后的p_go_micro.MicroService
func NewService(argsSvc p_go_micro.MicroServiceInfo, argsPath ...string) p_go_micro.MicroServiceInfo {
	var filePath = tools.PathGetRoot(ProjectName) + "\\internal\\config"
	if os.Getenv("ENV_DIR") != "" {
		filePath = "/app/internal/config"
	}
	if argsPath != nil && len(argsPath) != 0 {
		filePath = argsPath[0]
	}
	log.Warnf("file path: %s", filePath)
	defaultSvc := p_go_micro.MicroServiceInfo{
		Name:    argsSvc.Name,
		Version: Version,
		ConfigPath: map[uint8]string{
			p_go_micro.ConstConfigTypeCache:           filePath + "/cache.yaml",
			p_go_micro.ConstConfigTypeDB:              filePath + "/db.yaml",
			p_go_micro.ConstConfigTypeServerDiscovery: filePath + "/service_discovery.yaml",
		},
		ServerDiscoveryType: cclouddefine.ConstServiceDiscoveryTypeZookeeper,
		ServerType:          p_go_micro.ConstServerTypeDefault,
		UseDBSqlType:        cclouddefine.ConstDBSqlTypePostgresql,
		UseDBNosqlType:      cclouddefine.ConstDBNoSqlTypeMongoDB,
		UseCacheType:        cclouddefine.ConstCacheTypeRedis,
	}
	if argsSvc.Name == "" {
		panic("service name is empty")
	}
	if argsSvc.Version != "" && argsSvc.Version != Version {
		defaultSvc.Version = argsSvc.Version
	}
	if argsSvc.ConfigPath != nil {
		defaultSvc.ConfigPath = argsSvc.ConfigPath
	}
	if argsSvc.ServerDiscoveryType != 0 {
		defaultSvc.ServerDiscoveryType = argsSvc.ServerDiscoveryType
	}
	if argsSvc.ServerType != p_go_micro.ConstServerTypeDefault {
		defaultSvc.ServerType = argsSvc.ServerType
	}
	if argsSvc.UseDBSqlType != 0 {
		defaultSvc.UseDBSqlType = argsSvc.UseDBSqlType
	}
	if argsSvc.UseDBNosqlType != 0 {
		defaultSvc.UseDBNosqlType = argsSvc.UseDBNosqlType
	}
	if argsSvc.UseCacheType != 0 {
		defaultSvc.UseCacheType = argsSvc.UseCacheType
	}
	return defaultSvc
}
